package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Brand;
import com.x.erp.rpc.BrandRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌Controller Created by gdf on 2018/1/26.
 */
@RestController
public class BrandController extends BaseController {

	@Reference
	private BrandRpcService brandRpcService;// 品牌服务

	/**
	 * 查询品牌列表
	 * 
	 * @param brand
	 * @return
	 */
	@RequestMapping(value = "/getBrandList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getBrandList(Brand brand) {
		List<Brand> brandList = brandRpcService.selectBrandList(brand);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", brandList);
	}

	/**
	 * 查询品牌信息（分组）
	 * 
	 * @param brand
	 * @return
	 */
	@RequestMapping(value = "/getGroupBrand", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getGroupBrand(Brand brand) {
		List<Map<String, Object>> resObj = new ArrayList<>();
		Map<String, List<Brand>> res = new HashMap<>();
		List<Brand> brands = brandRpcService.selectBrandList(brand);
		if (brands != null) {
			// 处理首字母
			for (Brand b : brands) {
				if (res.get(b.getBrandInitial()) == null) {
					List<Brand> bs = new ArrayList<>();
					bs.add(b);
					res.put(b.getBrandInitial(), bs);
				} else {
					res.get(b.getBrandInitial()).add(b);
				}
			}
		}
		for (String key : res.keySet()) {
			Map<String, Object> resMap = new HashMap<>();
			resMap.put("initial", key);
			resMap.put("brandInfo", res.get(key));
			resObj.add(resMap);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, resObj);
	}
}
